extends Node2D
var utils = preload("res://assets/gs/com.gd").new()

var mod_scene = load("res://mods/mod.tscn")
func _ready() -> void:
	$ModTimer.timeout.connect(_on_mods_make)
	
	print("current_formation")
	var current_formation = Init.getFormatItemById(Init.save_data_global.current_formation.rel_id)
	print(current_formation)
	var format_scene = load(current_formation.scn_path)
	var new_format = format_scene.instantiate()
	#new_format.set_editable(true)
	add_child(new_format)
	var w = utils.get_collision_polygon_size(new_format.get_node("CollisionPolygon2D")).x
	var x = $Marker2D.position.x-w
	new_format.global_position = Vector2(x,$Marker2D.position.y) 
	
	var current_roles = Init.save_data_global.current_formation.roles
	for roleItem in current_roles:
		var role_path = Init.getRoleItemById(roleItem.rel_id).scn_path
		var role_scn = load(role_path)
		var new_role = role_scn.instantiate()
		new_format.add_child(new_role)
		var marker_path = "Marker" + str(roleItem.marker)
		new_role.global_position  = new_format.get_node(marker_path).global_position
		
		print("测试",Init.getMyRoleItemById(roleItem.rel_id))
		var roleAllSkill = Init.getMyRoleItemById(roleItem.rel_id).equipped_skills
		var skill_i = 0
		for skill_id in roleAllSkill:
			skill_i += 1 
			var skill_item = Init.getSkillItemById(skill_id)
			var skill_scn = load(skill_item.scn_path)
			var newSkill = skill_scn.instantiate()
			new_role.add_child(newSkill)
			newSkill.global_position = new_role.get_node("MarkerSkill"+str(skill_i)).global_position


	
	#给角色配置技能

	
	$ModTimer.start()
	
func _on_mods_make():
	
	var mod = mod_scene.instantiate()
	var mod_spawn_location = $ModPath/ModPathFollow
	mod_spawn_location.progress_ratio = randf()
	mod.position = mod_spawn_location.position

	## Set the mob's direction perpendicular to the path direction.
	#var direction = mod_spawn_location.rotation + PI / 2
#
	## Add some randomness to the direction.
	#direction += randf_range(-PI / 4, PI / 4)
	#mod.rotation = direction
#
	## Choose the velocity for the mob.
	#var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
	#mod.linear_velocity = velocity.rotated(direction)

	# Spawn the mob by adding it to the Main scene.
	add_child(mod)
